package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.constant.StatusConstant;
import org.example.demo.entity.Promotion;
import org.example.demo.entity.User;
import org.example.demo.entity.UserStatus;
import org.example.demo.mapper.PromotionMapper;
import org.example.demo.mapper.UserMapper;
import org.example.demo.mapper.UserStatusMapper;
import org.example.demo.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 晋升记录服务实现类
 */
@Service
public class PromotionServiceImpl extends ServiceImpl<PromotionMapper, Promotion>
        implements PromotionService {

    @Autowired
    PromotionMapper promotionMapper;

    @Autowired
    UserMapper userMapper;


    @Autowired
    UserStatusMapper userStatusMapper;
    @Override
    public Page<Promotion> selectPage(Promotion promotion, Integer pageNum, Integer pageSize) {
        Page<Promotion> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        QueryWrapper<Promotion> queryWrapper = new QueryWrapper<>();

        // 构建查询条件
        queryWrapper.lambda()
                .eq(promotion.getUserId() != null, Promotion::getUserId, promotion.getUserId())
                .eq(promotion.getDeptId() != null, Promotion::getDeptId, promotion.getDeptId())
                .eq(promotion.getType() != null && !promotion.getType().isEmpty(),
                        Promotion::getType, promotion.getType())
                .orderByDesc(Promotion::getCreateTime);

        return this.page(page, queryWrapper);
    }


    /**
     * 根据userId返回promotion
     * @param userId
     * @return
     */
    public List<Promotion> getByUserId(Long userId) {

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.
                eq("user_id", userId);

        List promotions = promotionMapper.selectList(wrapper);
        return promotions;
    }

    @Override
    public boolean save(Promotion entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return super.save(entity);
    }

    @Override
    public boolean updateById(Promotion entity) {
        entity.setUpdateTime(LocalDateTime.now());

        // 审核状态通过
        if (entity.getStatus() == StatusConstant.PASSED) {
            User user = userMapper.selectById(entity.getUserId());
            user.setPoliticalStatus(entity.getType());

            // 更新用户状态
            userMapper.updateById(user);

            // 把用户更新状态数据库表更新
            UserStatus userStatus = UserStatus.builder()
                    .userId(user.getId())
                    .status(user.getPoliticalStatus())
                    .updateTime(LocalDateTime.now())
                    .build();

            userStatusMapper.insert(userStatus);

        }

        return super.updateById(entity);
    }


}